package test.updater;

import static junit.framework.Assert.*;

import java.text.ParseException;


import org.junit.Before;
import org.junit.Test;

import updater.MapShareVersionInfo;
import updater.MapShareVersionNumber;


public class VersionInfoShould {
	
	private MapShareVersionInfo info;

	@Before
	public void init(){
		info = new MapShareVersionInfo();
	}
	
	@Test
	public void parsesVersionNumbersProperly() throws ParseException{
		MapShareVersionNumber num = info.parseVersionNumber("1.2.3.4");
		assertEquals(1, num.MAJOR);
		assertEquals(2, num.MINOR);
		assertEquals(3, num.ISSUE);
		assertEquals(4, num.BUILD);
	}
	
	@Test(expected = ParseException.class)
	public void notParseVersionNumbersWithTooManyPeriods() throws ParseException{
		@SuppressWarnings("unused")
		MapShareVersionNumber num = info.parseVersionNumber("1.2.3.....4");
	}
	
	@Test
	public void compareVersionsProperly() throws ParseException{
		compareBuildNumber("1.1.1.1", "1.1.1.2");
		compareBuildNumber("1.1.1.1", "1.1.2.1");
		compareBuildNumber("1.1.1.1", "1.2.1.1");
		compareBuildNumber("1.1.1.1", "2.1.1.1");
		compareBuildNumber("10.10.10.10", "10.10.10.11");
		compareBuildNumber("10.10.10.10", "10.10.11.10");
		compareBuildNumber("10.10.10.10", "10.11.10.10");
		compareBuildNumber("10.10.10.10", "11.10.10.10");
		compareBuildNumber("100.100.100.100", "100.100.100.101");
		compareBuildNumber("100.100.100.100", "100.100.101.100");
		compareBuildNumber("100.100.100.100", "100.101.100.100");
		compareBuildNumber("100.100.100.100", "101.100.100.100");
		compareBuildNumber("1000000.1000000.1000000.1000000", "1000000.1000000.1000000.1000001");
		compareBuildNumber("1000000.1000000.1000000.1000000", "1000000.1000000.1000001.1000000");
		compareBuildNumber("1000000.1000000.1000000.1000000", "1000000.1000001.1000000.1000000");
		compareBuildNumber("1000000.1000000.1000000.1000000", "1000001.1000000.1000000.1000000");		
		compareBuildNumber("1.9.7.426", "2.0.0.1");
	}

	private void compareBuildNumber(String lesserVersion, String greaterVersion) throws ParseException {
		MapShareVersionNumber lesser 	= new MapShareVersionNumber(lesserVersion);
		MapShareVersionNumber greater 	= new MapShareVersionNumber(greaterVersion);
		assertTrue("Comparison of '"+lesser+ "' < '"+greater+"' failed",lesser. compareTo(greater)  < 0);
		assertTrue("Comparison of '"+greater+"' > '"+lesser+ "' failed",greater.compareTo(lesser)   > 0);
		assertTrue("Comparison of '"+lesser+ "' = '"+lesser+ "' failed",lesser. compareTo(lesser)  == 0);
		assertTrue("Comparison of '"+greater+"' = '"+greater+"' failed",greater.compareTo(greater) == 0);
	}
}
